package cheme.ProcessEditor.Policies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.jface.viewers.CellEditor;

import cheme.ProcessEditor.DirectEdit.ListCellEditor;
import cheme.ProcessEditor.Parts.ISelectionModelPart;
import cheme.ProcessModel.Cmds.SetModelSelectionCmd;
import cheme.ProcessModel.Table.ISelectionModel;

public class PolicySelectionFieldDirectEdit extends DirectEditPolicy
{
	Integer oldValue;
	
	/**
	 * @see DirectEditPolicy#getDirectEditCommand(org.eclipse.gef.requests.DirectEditRequest)
	 */
	protected Command getDirectEditCommand(DirectEditRequest request)
	{
		ISelectionModel model = (ISelectionModel) getHost().getModel();
		ListCellEditor cellEditor = (ListCellEditor)request.getCellEditor();
		
		return new SetModelSelectionCmd(model, (Integer)cellEditor.getValue());
	}

	/**
	 * @see DirectEditPolicy#showCurrentEditValue(org.eclipse.gef.requests.DirectEditRequest)
	 */
	protected void showCurrentEditValue(DirectEditRequest request)
	{
		Integer value = (Integer) request.getCellEditor().getValue();
		ISelectionModelPart part = (ISelectionModelPart)getHost();
		part.handleSelectionChange(value);
	}
	
	/**
	 * @param to Revert request
	 */
	protected void storeOldEditValue(DirectEditRequest request)
	{
		CellEditor cellEditor = request.getCellEditor();
		oldValue = (Integer) cellEditor.getValue();
	}

	/**
	 * @param request
	 */
	protected void revertOldEditValue(DirectEditRequest request)
	{
//		CellEditor cellEditor = request.getCellEditor();
//		cellEditor.setValue(oldValue);
		ISelectionModelPart part = (ISelectionModelPart)getHost();
		part.revertSelectionChange(oldValue);
	}
}
